package com.sun.mybatisplus.controller;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.sun.mybatisplus.bean.User;
import com.sun.mybatisplus.service.UserService;
import io.swagger.annotations.ApiModelProperty;
import org.junit.Test;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user/")
public class LoginController {
    @Resource
    UserService userService;

    // 测试登录，浏览器访问： http://localhost:8081/user/doLogin?username=zhang&password=123456
    @RequestMapping("doLogin")
    public String doLogin(String username, String password) {
        // 此处仅作模拟示例，真实项目需要从数据库中查询数据进行比对
        if ("zhang".equals(username) && "123456".equals(password)) {
            StpUtil.setLoginId(username, "pc");
            SaSession session = StpUtil.getTokenSession();
            session.setAttribute("user", username);
            session.setAttribute("permissions", "123");
            SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
            System.out.println(tokenInfo.toString());
            return "登录成功";
        }
        return "登录失败";
    }

    @RequestMapping("add")
    public String addUser(@RequestBody User user){
        userService.save(user);
        return "succ";
    }

    // 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
    @RequestMapping("isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }

    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        User u1 = new User();
        u1.setId(1L);
        u1.setAge(20);
        u1.setAgss("1");
        User u2 = new User();
        u2.setId(1L);
        u2.setAgss("3");
        Class class1 = u1.getClass();
        Class class2 = u2.getClass();
        Map<String, String> map = new HashMap<>();
        Map<String, Object> keyAndVar = new HashMap<>();
        Field[] fields = class1.getDeclaredFields();

        for (Field field : fields) {
            field.setAccessible(true);
            ApiModelProperty api = field.getAnnotation(ApiModelProperty.class);
            try {
                if (!StringUtils.isEmpty(api.value()) && field.get(u1) != null) {
                    map.put(field.getName(), api.value());
                    keyAndVar.put(field.getName(), field.get(u1));
                }

            } catch (Exception e) {
                System.out.println("这个属性没有注解");
            }

        }
        Field[] fields2 = class2.getDeclaredFields();
        for (Field field : fields2) {
            field.setAccessible(true);
            if (!keyAndVar.containsKey(field.getName())) {
                continue;
            }
            try {
                System.out.println(field.get(u2));
                if (!keyAndVar.get(field.getName()).equals(field.get(u2)) && field.get(u2) != null) {
                    sb.append("属性值:");
                    sb.append(map.get(field.getName()));
                    sb.append("从");
                    sb.append(keyAndVar.get(field.getName()));
                    sb.append("变为-->");
                    sb.append(field.get(u2));
                }
            } catch (IllegalAccessException e) {
                continue;
            }
        }
        System.out.println(map);
        System.out.println(keyAndVar);
        System.out.println(sb.toString());
    }

    @Test
    public void test1(){
        String str = "[3001] 部门支出";
        StringBuilder sb1 = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();

        for (char s : str.toCharArray()) {
            if (Character.isDigit(s)) {
                sb1.append(s);
            }else {
                sb2.append(s);
            }
        }

        System.out.println(sb1);
        System.out.println(sb2);

    }

    @Test
    public void test2(){
        User user = new User();
        List<User> users = new ArrayList<>();
        users.add(user);
        users.add(user);
        for (User user1 : users) {
            System.out.println(user1.getId().toString());
        }

    }

}
